<template>
	<view class="yang_zhi_dang_an_box" :style="{height: windowHeight + 'px'}">
		<!-- 标题 -->
		<view class="title_box pv_20 text_center">
			<text class="title vertical_text mn fs_36_i color_fff">ᠲᠡᠵᠢᠭᠡᠨ ᠦᠷᠡᠵᠢᠭᠦᠯᠬᠦ ᠳᠠᠩᠰᠠ ᠡᠪᠬᠡᠮᠡᠯ</text>
			
			<view class="add_btn">
				<i class="iconfont icon-tianjiatupianlimianjiahao color_fff fs_40_i" @click="addYangZhi(activeTabName)"></i>
			</view>
		</view>
		
		<!-- 内容 -->
		<view class="content_box">
			<!-- tab 栏 -->
			<view class="tab_box w_full">
				<view class="tab_list">
					<view class="tab_item" 
						:class="{ active_tab : activeTabName == index }" 
						v-for="(item, index) in animalCategoryList" :key="index"
						@click="handleClickAnimalTab(index, item.animalId)"
					>
						<text class="tab_name vertical_text mn fs_40">{{ item.animalNameMn }}</text> 
						<text class="num">({{ item.animalCount }})</text> 
					</view>
				</view>
			</view>
			
			<!-- 养殖 list -->
			<view class="yang_zhi_list">
				<scroll-view class="full" scroll-y="true" lower-threshold="50" @scrolltolower="handlePagenation()">
					<view class="yang_zhi_item" v-for="(item, index) in yangZhiList" :key="index" @click="handleClickItem(item.id)">
						<view class="img_box" v-if="item.files && item.files.length">
							<image :src="IMG_URL + item.files[0].path || def_avatar_img" mode=""></image>
						</view>
						<view class="img_box" v-else>
							<!-- <image :src="def_avatar_img" mode=""></image> -->
							<i class="iconfont icon-tianjiatupian1 color_ccc fs_140_i"></i>
						</view>
						
						<view class="yang_zhi_content">
							<text class="vertical_text mn fs_36">ᠨᠠᠰᠤ : {{ item.age }}</text>
							<text class="vertical_text mn fs_36">ᠬᠦᠢᠰᠦ : {{ formatSex(item.sex) }}</text>
							<text class="vertical_text mn fs_36">ᠦᠷᠡᠵᠢᠯᠲᠡ᠎ᠶᠢᠨ ᠲᠣᠭ᠎ᠠ ᠬᠡᠮᠵᠢᠶ᠎ᠡ : {{ item.breedingCount }}</text>
							<text class="vertical_text mn fs_36">ᠬᠢᠵᠢᠭ ᠪᠠᠢᠴᠠᠭᠠᠬᠤ ᠤᠳᠠᠭ᠎ᠠ: {{ item.inspection }}</text>
						</view>
					</view>
				</scroll-view>	
			</view>
			
		</view>
	</view>
</template>

<script>
	import getSystemInfo from '../../mixins/basic.js'
	import NET from '@/utils/request.js'
	import API from '@/config/api.js'
	
	export default {
		mixins: [getSystemInfo],
		data() {
			return {
				activeTabName: 0,
				activeTabId: '',
				animalCategoryList: [],
				
				yangZhiList: [],
				yangZhiTotal: 0,
				pagenation: {
					current: 1,
					size: 10
				},
				isPagenatiion: false
			}
		},
		onLoad() {
			// 获取设备高度
			this.getSystemHeight()
		},
		onShow() {
			
		},
		mounted() {
			this.getAnimalCategoryList()
			
			setTimeout(() => {
				if (!this.yangZhiList.length) {
					this.pagenation.current = 1
					this.getYangZhiListByAnimalId(this.activeTabId)
				}
			}, 300)
		},
		methods: {
			// 获取牲畜分类
			getAnimalCategoryList() {
				NET.request(API.getAnimalCategory, 'GET')
				.then(res => {
					// console.log('获取牲畜列表 : ', res)
					if (res.code == 200) {
						this.animalCategoryList = res.data
						this.activeTabId = res.data[0].animalId
					}
				})
				.catch(err => {
					console.error('获取牲畜列表失败', err)
				})
			},
			
			// 点击 animalTabName
			handleClickAnimalTab(index, id) {
				this.activeTabName = index
				this.activeTabId = id
				
				// 清空当前养殖档案
				this.yangZhiList = []
				
				// 获取牲畜分类下的养殖档案
				this.getYangZhiListByAnimalId(id)
			},
			
			// 获取牲畜分类下的养殖档案
			getYangZhiListByAnimalId(id) {
				let params = { 
					animalId: this.activeTabId || id,
					current: this.pagenation.current,
					size: this.pagenation.size
				}
				
				NET.request(API.getYangZhiIndex, params, 'GET')
				.then(res => {
					if (res.code == 200) {
						this.yangZhiList = [...this.yangZhiList, ...res.data.list]
						this.yangZhiTotal = res.data.totalCount
					}
				})
				.catch(err => {
					console.error('获取养殖档案首页失败', err)
				})
			},
			
			// 养殖档案分页
			handlePagenation() {
				if (!this.isPagenation) {
					if (this.yangZhiList.length < this.yangZhiTotal) {
						this.isPagenation = true
						this.yangZhiList.current ++
						this.getYangZhiListByAnimalId(id)
						setTimeout(() => {this.isPagenation = false}, 500)
					} else {
						uni.showToast({
							title: '数据到底了！', icon: 'none'
						})
					}
				} else {
					return false
				}
			},
			
			// 点击 item 查看养殖档案详情
			handleClickItem(id) {
				uni.navigateTo({
					url: `/subcontract/yang-zhi-dang-an/yang-zhi-detail?id=${id}`,
				})
			},
			
			// 点击 添加图标 跳转到 添加养殖档案页面
			addYangZhi(val) {
				uni.navigateTo({
					url: `/subcontract/yang-zhi-dang-an/add-yang-zhi?name=${val}`,
				})
			},
			
			
		}
	}
</script>

<style lang="less" scoped>
	.active_tab { border-bottom: 8rpx solid #ffa500; }
	
	.yang_zhi_dang_an_box {
		width: 100%;
		// border: 1px solid #f00;
		box-sizing: border-box;
		position: relative;
		
		// 标题
		.title_box {
			width: 80rpx;
			height: 100%;
			background-color: #1c9aa0;
			box-sizing: border-box;
			position: relative;
			float: left;
			
			.title {}
			.add_btn {
				width: 100%;
				height: 50rpx;
				position: absolute;
				bottom: 30rpx;
				
			}
		}
	
		// 内容
		.content_box {
			width: calc(100% - 80rpx);
			height: 100%;
			float: left;
			padding: 20rpx 0;
			box-sizing: border-box;
			
			.tab_box {
				width: 100%;
				height: 110rpx;
				.tab_list {
					width: 100%;
					height: 100%;
					display: flex;
					flex-direction: row;
					justify-content: space-around;
					
					// 添加滚动
					// overflow-x: auto;
					// overflow-y: hidden;
					// white-space: nowrap;
					
					.tab_item {
						width: 60rpx;
						height: 100%;
						// border: 1px solid #f00;
						// display: inline-block;
						position: relative;
						text-align: center;
						
						.num {
							position: absolute;
							left: 50%;
							bottom: 5rpx;
							transform: translateX(-50%);
						}
					}
				}
			}
		
			.yang_zhi_list {
				width: 100%;
				height: calc(100% - 100rpx);
				// border: 1px solid #080;
				margin-top: 10rpx;
				margin-left: 20rpx;
				overflow-x: auto;
				overflow-y: hidden;
				white-space: nowrap;
				box-sizing: border-box;
			
				.yang_zhi_item {
					width: 160rpx;
					height: 100%;
					padding: 10rpx;
					// float: left;
					// border: 1px solid #f00;
					display: inline-block;
					box-sizing: border-box;
					
					.img_box {
						width: 100%;
						height: 140rpx;
						
						image { width: 100%; height: 100%; border-radius: 10rpx; }
					}
					
					.yang_zhi_content {
						width: 100%;
						height: calc(100% - 150px);
						display: flex;
						flex-direction: row;
						justify-content: space-around;
						padding-top: 10rpx;
					}
					
				}
			}
		}
	}
</style>